// pages/guide/index.js
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    exits: true,
    userType: '00', // 用户类型 00经营主体 01批发商 02零售商
    account: '', // 账号
    password: '', // 密码
    sanhu:{
      compnayName:'',//商家名称
      name:'',//散户姓名 
      phone:''//散户手机号
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onLoad(options) {
    wx.clearStorageSync()
    // 存在token不重复调用
    const token =  wx.getStorageSync('token')

    if (token) {
      // 存在token  直接前往首页
      wx.redirectTo({ url: '/pages/index/index' })
    } else {
      this.getCode()
    }

  },

  // 切换
  onChange (e) {
    this.setData({ userType: e.detail })
  },
  bindAccount(e){
    const value = e.detail; //值 
    this.setData({
      account:value
    })
    console.log("u:"+this.data.account)
  },
  bindPwd(e){
    const value = e.detail; //值 
    this.setData({
      password:value
    })
  },

  /**
   * 经营主体登录
   */
  LoginUser(){
    console.log("u:"+this.data.account+";p:"+this.data.password)
    if (!this.data.account) {
      wx.showToast({  title: '请输入用户名', icon: 'error' })
      return 
    }
    if (!this.data.password) {
      wx.showToast({  title: '请输入密码', icon: 'error' })
      return 
    }
    wx.showLoading({ title: '登录中' })
    // 经营主体账号密码登录
    wx.$req({
      url: '/login',
      data: {
        username: this.data.account,
        password: this.data.password,
        openId: wx.getStorageSync('openid'),
      },
      tokenFlag: false,
      method: 'POST'
    }).then((res) => {
      // 缓存新的token 
      wx.setStorageSync('token', res.data.token)
      wx.setStorageSync('name', res.data.username)
      
      this.getCode()
      // wx.redirectTo({ url: '/pages/index/index'  })
    }).finally(() => { 
      console.log(555)
      setTimeout(()=>{
        wx.hideLoading() 

      },2000)
    })
},

  // 确认(经营主体登录)
  confirm () {
    if(this.data.userType=='00'){
      this.LoginUser()
    }else{
      this.improveUserInformation()
    }
  },
  improveUserInformation(){
    let that=this
      wx.$req({
        url: '/wms/wechat/improveUserInformation',
        data: {companyName:that.data.sanhu.compnayName,name:that.data.sanhu.name,phone:that.data.sanhu.phone ,userType:that.data.userType },
        method:'POST',
        loadingText: '加载中...'
      }).then(res => {
         console.log("res:",res)
         if(res.data){
           //成功
           this.getCode()
          //  wx.redirectTo({ url: '/pages/index/index'  })
         }
      })
  },
  bindCompnayName(e){
        const value = e.detail; //值 
        this.setData({
          'sanhu.compnayName':value
        })
  },
  bindName(e){
    const value = e.detail; //值 
    this.setData({
      'sanhu.name':value
    })
  },
  bindPhone(e){
    const value = e.detail; //值 
    this.setData({
      'sanhu.phone':value
    })
  },
  // 通过wx.login获取的code 换取openid,与token
  getCode () {
    wx.login({
      success: (res) => {
        if (res.code) {
          //发起网络请求
          wx.$req({
            url: '/wms/wechat/WeChatLogin',
            data: {code: res.code},
            tokenFlag: false,
            loadingText: '加载中...'
          }).then(res => {
            const { exits, token, userType, openid ,name,postsNames,qrcode,userId} = res.data
            // 用户类型 00经营主体 01批发商 02零售商
            // 缓存token userType username
            wx.setStorageSync('token', token)
            wx.setStorageSync('userType', userType)
            wx.setStorageSync('openid', openid)
            wx.setStorageSync('name', name)
            wx.setStorageSync('userId', userId)
            wx.setStorageSync('postsNames', postsNames)
            wx.setStorageSync('qrcode', qrcode)
            this.setData({ userType })
            if(exits){
              wx.reLaunch({ url: '/pages/index/index'  })
            }
            /**
             * exits 为false 将掉起登录弹窗
             * 经营主体，需要账号密码登录
             * 散户（零售商 批发商） 直接补全身份信息即可
            */
            this.setData({ exits,userType:"00" })

          })
        } else {
          wx.showToast({  title: '请求失败请重试', icon: 'error'})
        }
      }
    })
  }

})